package hk.m4s.lr.repair.test.ui.adapter;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import framework.common.Constant;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.AddressModel;
import hk.m4s.lr.repair.test.model.AddressModelResult;
import hk.m4s.lr.repair.test.service.address.AddressService;
import hk.m4s.lr.repair.test.ui.user.AddAddressActivity;
import okhttp3.Response;

public class AddressListAdapter extends BaseAdapter {
	private Activity context;
	public List<AddressModel> list;
	private LayoutInflater flater;
	Bitmap bi = null;
	String flags;
	ListView listView;
	private ProgressDialog progress;
	public AddressListAdapter(Activity context, List<AddressModel> list) {
		this.context = context;
		this.list = list;
		this.flater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = flater.inflate(R.layout.list_item_address, null);
			holder.phone = (TextView) convertView
					.findViewById(R.id.user_phone);
			holder.address_ad = (TextView) convertView
					.findViewById(R.id.user_adrress);
			holder.marketname = (TextView) convertView
					.findViewById(R.id.shop_user_name);
			holder.shop_et = (TextView) convertView
					.findViewById(R.id.shop_et);
			holder.shop_adrress_check = (TextView) convertView
					.findViewById(R.id.shop_adrress_check);
            holder.shop_delete= (TextView) convertView
					.findViewById(R.id.shop_delet);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.shop_delete.setTag(position);
		holder.shop_et.setTag(position);
		holder.shop_adrress_check.setTag(position);
        if(list!=null){
			AddressModel bean=list.get(position);
			holder.marketname.setText(bean.getName());
			holder.phone.setText(bean.getTel());
            holder.address_ad.setText(bean.getAddress());
			if(bean.getState()==2){
				Drawable rightDrawable = context.getResources().getDrawable(R.mipmap.select_address_yes);
				rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
				holder.shop_adrress_check.setCompoundDrawables(rightDrawable, null,null , null);
			}else{
				Drawable rightDrawable = context.getResources().getDrawable(R.mipmap.select_address_no);
				rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
				holder.shop_adrress_check.setCompoundDrawables(rightDrawable, null,null , null);
			}
		}
		holder.shop_et.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				int num=(Integer) view.getTag();
				AddressModel bean=list.get(num);
				Intent in=new Intent(context,AddAddressActivity.class);
				in.putExtra("username",bean.getName());
				in.putExtra("phone",bean.getTel());
				in.putExtra("keycode",bean.getId());
				in.putExtra("pro",bean.getProvince());
				in.putExtra("state",bean.getState());
				in.putExtra("title_name","编辑收货地址");
				in.putExtra("address",bean.getAddress());
				context.startActivity(in);
			}
		});
		holder.shop_adrress_check.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				int num=(Integer) view.getTag();
				AddressModel bean=list.get(num);


					for (int i=0;i<list.size();i++){
						if(num==i){
							if(bean.getState()==2) {
								list.get(num).setState(1);
								notifyDataSetChanged();
							}else{
								list.get(i).setState(2);
								notifyDataSetChanged();
								setState(bean.getId());
							}
						}else{
							list.get(i).setState(1);
							notifyDataSetChanged();
						}

					}

			}
		});
		holder.shop_delete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				int num=(Integer) view.getTag();
				AddressModel bean=list.get(num);
				deleteAdress(bean.getId());
				list.remove(num);
				notifyDataSetChanged();
			}
		});
		return convertView;
	}

	static class ViewHolder {
		public TextView shop_adrress_check;
		public TextView shop_delete;
		public TextView shop_et;
		public TextView marketname;
		public TextView phone;
		public TextView address_ad;
	}

	public void setState(final int id){
		Map<String, Object> parmas = new HashMap<>();
		parmas.put("device_type", Constant.device_type);
		parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
		try {
			JSONObject jsondata = new JSONObject();
			jsondata.put("id", id);
			parmas.put("jsonText",jsondata.toString());
		}catch (JSONException e){

		}
		AddressService.expressAddressState(context,parmas , new ResponseCallback<AddressModel>() {
			@Override
			public void onSuccess(AddressModel deviceModelResult) {

			}

			@Override
			public void onError(Response response, int errorCode, String errorMsg, Exception e) {

			}
		});
	}
	public void deleteAdress(final int id){
		Map<String, Object> parmas = new HashMap<>();
		parmas.put("device_type", Constant.device_type);
		parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
		try {
			JSONObject jsondata = new JSONObject();
			jsondata.put("id", id);
			parmas.put("jsonText",jsondata.toString());
		}catch (JSONException e){

		}
		AddressService.expressAddressDel(context,parmas , new ResponseCallback<AddressModel>() {
			@Override
			public void onSuccess(AddressModel deviceModelResult) {

			}

			@Override
			public void onError(Response response, int errorCode, String errorMsg, Exception e) {

			}
		});
	}
}
